package utils;

import android.content.Context;

import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.io.File;
import java.io.IOException;

import http.AsyncHttpUtil;
import log.LogManager;

/**
 * Created by hqw on 2015/9/11.
 */
public class UploadFileUtil {
    private static UploadFileUtil _uploadFileUtil;

    public static UploadFileUtil getInstance() {
        if (_uploadFileUtil == null) {
            _uploadFileUtil = new UploadFileUtil();
        }
        return _uploadFileUtil;
    }
    /**
     * 下载音频文件
     */
    public  void uploadFileVoice(Context context, String url, final UploadCallBack callBack){
        LogManager.getLogger().l("HTTP url=" + url);
        int fileindex = url.lastIndexOf("/") + 1;

        final String filePath = PhotoUtil.getVoicePath(url.substring(fileindex, url.length()-4));
        //final String filePath = FileUtil.MAR_DIR_VOICE+url.substring(fileindex, url.length() - 4)+".aac";

        LogManager.getLogger().l("HTTP url-filePath =" + filePath);
        if (PhotoUtil.isFileExist(filePath)){
            callBack.onSuccess(filePath);
        }else {
            AsyncHttpUtil.get(context, url, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    try {
                        FileUtil.writeByteArrayToFile(new File(filePath),responseBody);
                        callBack.onSuccess(filePath);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    callBack.onSuccess(null);
                }

            });
        }
    }


    /**
     * 下载视频文件
     */
    public  void uploadFileVideo(Context context, String url, final UploadCallBack callBack){
        if (PhotoUtil.isFileExist(url)){
            callBack.onSuccess(url);
        }else {
            final String filePath = PhotoUtil.getVideoPath(AESEncryptUtils.md5String(url));
            if (PhotoUtil.isFileExist(filePath)){
                callBack.onSuccess(filePath);
            }else {
                AsyncHttpUtil.get(context, url, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        try {
                            FileUtil.writeByteArrayToFile(new File(filePath), responseBody);
                            callBack.onSuccess(filePath);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        //callBack.onSuccess(null);
                    }

                });
            }
        }
    }

    public static abstract class UploadCallBack{
        public void onSuccess(String filePath){

        }
    }

}
